package com.wcgksoft.controller.insect;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.insect.InsectDetection;
import com.wcgksoft.entity.insect.InsectHistory;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.service.insect.InsectDetectionService;
import com.wcgksoft.service.insect.InsectHistoryService;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 *
 * 虫情地图
 *
 */

@Controller
@RequestMapping("insect/insectMap")
public class InsectMapController {


    @Autowired
    private InsectDetectionService insectDetectionService;

    @Autowired
    private  InsectHistoryService insectHistoryService;


    /**虫情地图，查监测点*/
    @ResponseBody
    @RequestMapping(value = "getAll")
    public Layui getAll(HttpServletRequest request,
                 @RequestParam(value = "releaseTime",required = false) String releaseTime) {

        EnterpriseProject enterpriseProject =(EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(InsectDetection.class);
        if(null!=releaseTime&&!"".equals(releaseTime)){
            criteria.add(Restrictions.sqlRestriction("DATE_FORMAT(time,'%Y')= '"+releaseTime+"'"));
        }
        criteria.add(Restrictions.eq("areaId",enterpriseProject.getId()));
        List<InsectDetection> list = insectDetectionService.findByConditions(criteria);
        return Layui.data(0, "查询成功", insectDetectionService.getTotalCount(), list);
    }



    /**虫情地图，查历史病虫害点*/
    @ResponseBody
    @RequestMapping(value = "find")
    public Layui find(HttpServletRequest request,
                      @RequestParam(value = "releaseTime",required = false) String releaseTime) {

        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(InsectHistory.class);
        if(null!=releaseTime&&!"".equals(releaseTime)){
            criteria.add(Restrictions.sqlRestriction("DATE_FORMAT(releaseTime,'%Y')= '"+releaseTime+"'"));
        }
        criteria.add(Restrictions.eq("areaId",enterpriseProject.getId()));
        List<InsectHistory> list = insectHistoryService.findByConditions(criteria);
        return Layui.data(0, "查询成功", insectHistoryService.getTotalCount(), list);
    }

}
